package com.ruoyi.Api;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.countrybus.domain.*;
import com.ruoyi.project.countrybus.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Api("调度API")
@RestController
@RequestMapping("/api/dispatch")
public class DispatchApi extends BaseController
{
    @Autowired
    private ISysDispatchService sysDispatchService;

    @Autowired
    private ISysDispatchService iSysDispatchService;

    @Autowired
    private ISysVehicleService iSysVehicleService;

    @Autowired
    private ISysDriverService iSysDriverService;

    @Autowired
    private ISysStationService iSysStationService;

    @Autowired
    private ISysStationRouteService iSysStationRouteService;

    @Autowired
    private ISysCustomerService iSysCustomerService;

    @Autowired
    private ISysDispatchCustomerService iSysDispatchCustomerService;


    /**
     * 查询调度信息列表
     */
    @ApiOperation("查询调度信息列表")
    @GetMapping("/list")
    public TableDataInfo list(SysDispatch sysDispatch)
    {
        startPage();
        List<SysDispatch> list = sysDispatchService.selectSysDispatchList(sysDispatch);
        return getDataTable(list);
    }


    /**
     * 获取调度信息详细信息
     */
    @ApiOperation("通过ID获取调度信息详细信息")
    @GetMapping(value = "/{dispatchId}")
    public AjaxResult getInfo(@PathVariable("dispatchId") Long dispatchId)
    {
        return AjaxResult.success(sysDispatchService.selectSysDispatchByDispatchId(dispatchId));
    }

    /**
     * 新增调度信息
     */
    @ApiOperation("新增调度信息")
    @PostMapping
    public AjaxResult add(@RequestBody SysDispatch sysDispatch)
    {
        return toAjax(sysDispatchService.insertSysDispatch(sysDispatch));
    }

    /**
     * 修改调度信息
     */
    @ApiOperation("修改调度信息")
    @PutMapping
    public AjaxResult edit(@RequestBody SysDispatch sysDispatch)
    {
        return toAjax(sysDispatchService.updateSysDispatch(sysDispatch));
    }

    /**
     * 删除调度信息
     */
    @ApiOperation("删除调度信息")
    @DeleteMapping("/{dispatchIds}")
    public AjaxResult remove(@PathVariable Long[] dispatchIds)
    {
        return toAjax(sysDispatchService.deleteSysDispatchByDispatchIds(dispatchIds));
    }


    @ApiOperation("查询调度所有信息")
    @GetMapping(value = "allinfo/{dispatchId}")
    public SysAllInfo getAllInfo(@PathVariable("dispatchId") Long dispatchId){
//        System.out.println("1111111111111");
//
        SysAllInfo sysAllInfo = new SysAllInfo();
//
////        System.out.println("3333333333333");
//        sysAllInfo.setSysDispatch(iSysDispatchService.selectSysDispatchByDispatchId(dispatchId));
//        sysAllInfo.setSysVehicle(iSysVehicleService.selectSysVehicleByVehicleId(sysAllInfo.getSysDispatch().getVehicleId()));
//        sysAllInfo.setSysDriver(iSysDriverService.selectSysDriverByDriverId(sysAllInfo.getSysVehicle().getDriverId()));
//        sysAllInfo.setSysStationRouteList(iSysStationRouteService.selectSysStationRouteByRouteId(sysAllInfo.getSysDispatch().getRouteId()));
//
//        List<SysStation> list = new ArrayList<>();
//        for (SysStationRoute sysStationRoute : sysAllInfo.getSysStationRouteList()) {
//            list.add(iSysStationService.selectSysStationByStationId(sysStationRoute.getStationId()));
//        }
//        sysAllInfo.setSysStationList(list);
//
////        System.out.println("2222222222222");
//        List<SysCustomer> listC = new ArrayList<>();
//        SysDispatchCustomer sysDispatchCustomerC = new SysDispatchCustomer();
//        sysDispatchCustomerC.setRouteId(sysAllInfo.getSysDispatch().getRouteId());
//        for (SysDispatchCustomer sysDispatchCustomer : iSysDispatchCustomerService.selectSysDispatchCustomerList(sysDispatchCustomerC)) {
//            listC.add(iSysCustomerService.selectSysCustomerByCustomerId(sysDispatchCustomer.getCustomerId()));
//        }
//        sysAllInfo.setSysCustomerList(listC);


        return sysAllInfo;
    }
}
